import type { IDeserializable } from "~/models/serialize/IDeserializable";
import { DataType } from "./type";
import { Deserializer } from "~/models/serialize/Deserializer";
import { Dzer } from "~/models/serialize";

// 定义: 接口类型 (和类型)：
// 不是一种具体的类型，而是类型的类型。在这里定义结构和生成器

export class UnionType
  extends DataType
  implements IDeserializable
{
  className: string = 'UnionType'

  static {
  }

  constructor(
    public parallels: DataType[] = []
  ) {
    super()
  }

  get name(): string {
    return this.parallels.map(x => x.name).sort().join('|')
  }

  get default(): any {
    return this.parallels[0].default
  }

  receiveType(t: DataType): boolean {
    // 同一类型则跳过后续判断
    if (t.name === this.name) {
      return true;
    }

    return this.parallels.map(x => x.receiveType(t)).some(x => x)
  }
  receiveValue(t: any): boolean { return this.parallels.some(x => x.receiveValue(t)) }
}

